package com.hdphone.zljutils.impl;

import android.view.View;

import com.hdphone.zljutils.R;
import com.hdphone.zljutils.ZljUtils;
import com.hdphone.zljutils.inter.IWidgetUtil;

/**
 * @date: 2021/6/3
 * @email： yzandlj@163.com
 * @author： yangzhi
 * @describe:
 */
public class WidgetUtilImpl implements IWidgetUtil {
    private static final String TAG = "WidgeUtils";

    /**
     * 组件点击事件间隔
     */
    public interface WidgetClickDuration {

        /**
         * 短间隔
         */
        public static final long SHORT_CLICK_DURATION = 800;

        /**
         * 长间隔
         */
        public static final long LONG_CLICK_DURATION = 1000;
    }

    /**
     * 检测是否在当前view上执行快速重复点击事件
     *
     * @param v
     * @return
     */
    @Override
    public boolean isFastClick(View v) {
        return isFastClick(v, WidgetClickDuration.SHORT_CLICK_DURATION);
    }

    /**
     * 检测是否在当前view上执行快速重复点击事件
     *
     * @param v
     * @param clickDuration 点击事件间隔
     * @return
     */
    private boolean isFastClick(View v, long clickDuration) {
        if (v == null) return false;
        long lastClickTime = 0;
        try {
            Long timeObj = (Long) v.getTag(R.id.item_last_click_time);
            if (timeObj != null) {
                //lastClickTime = timeObj;
                lastClickTime = timeObj.longValue();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        long spent = System.currentTimeMillis() - lastClickTime;
        ZljUtils.LOG().d(TAG, "now = " + System.currentTimeMillis() + " lastClickTime = " + lastClickTime + " ,spent = " + spent);
        if (System.currentTimeMillis() - lastClickTime >= clickDuration || spent <= 0) {
            lastClickTime = System.currentTimeMillis();
            v.setTag(R.id.item_last_click_time, lastClickTime); // 此处暂不使用setTag(Object obj),避免覆盖原有Tag逻辑
            return false;
        }

        return true;
    }
}
